package com.fiap.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.fiap.model.User;

public class DatabaseHelper extends SQLiteOpenHelper {

	private static final String TAG = "DatabaseHelper";
	
	private static final String DATABASE_NAME = "Runners";
	private static int VERSION = 1;

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		createUserTable(db);
		createRouteTable(db);
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}
	
	public void addUser(long id, String username, String email) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put("user_id", id);
        values.put("user_name", username);
        values.put("user_email", email);
 
        db.insert("runners_user", null, values);
        db.close();
    }
	
	public void updateUserInitialInfo(String weight, String height, Long id) {

		SQLiteDatabase db = this.getWritableDatabase();
        String strFilter = "user_id=" + id;
        
        ContentValues values = new ContentValues();
        values.put("user_weight", weight);
        values.put("user_height", height);
 
        db.update("runners_user", values, strFilter, null);
        db.close();
    }
	
	public int getRowCount() {
        String countQuery = "select * from runners_user";
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        int rowCount = cursor.getCount();
        db.close();
        cursor.close();
         
        return rowCount;
    }
	
	 /**
     * Re-crate database
     * Delete all tables and create them again
     * */
    public void resetTables(){
        SQLiteDatabase db = this.getWritableDatabase();
        // Delete All Rows
        db.delete("runners_user", null, null);
        db.close();
    }
    
    public User getUser(){ 
    	SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("select user_id, user_name, user_email from runners_user", null);
        if(cursor != null) {
        	cursor.moveToFirst();
        }
        
        Long id = cursor.getLong(0);
        String username = cursor.getString(1);
        String email = cursor.getString(2);
        
        
        User user = new User(id, username, email);
        db.close();
        return user;
    }
    
    /**
     * Main table of the application. Store the user information
     */
    public void createUserTable(SQLiteDatabase db){
    	db.execSQL("create table runners_user (" +
				"user_id integer primary key, " +
				"user_name text, " +
				"user_email text," +
				"user_weight text," +
				"user_height text);");
    }
    
    /**
     * 
     * Table create to store all the tracks (runs) that the users ran. 
     */
    public void createRouteTable(SQLiteDatabase db){
    	db.execSQL("create table runners_user_routes (" +
				"user_id integer primary key, " +
				"route_id integer primary key, " +
				"route_starttime datetime, " +
				"route_endtime email text," +
				"route_calburned real," +
				"route_avgspeed real," +
				"route_maxspeed real," +
				"route_distance real);");
    }
    
    /**
     * Table created to store user preferences, such as facebook accounts,
     * metrics system (kilometers & miles) etc. 
     */
    public void createUserPreferencesTable(SQLiteDatabase db){
    	db.execSQL("CREATE TABLE runners_user_preferences  (" +
				"user_id integer primary key, " +
				"sound_on_off type integer, " +
				"language_pref type text;");
    }

}
